//
//  ModifyUserinfoViewController.m
//  WeCommunity
//
//  Created by mac on 2017/6/20.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ModifyUserinfoViewController.h"
#import "Verificationlogic.h"
#import "UITextField+L_TextField.h"

#define  KMaxNum  12
#define  KMinNum  2

@interface ModifyUserinfoViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) UIBarButtonItem * rightBtnItem;
@property (nonatomic,strong) UITextField * textField;

@end

@implementation ModifyUserinfoViewController

#pragma mark - get
-(UIBarButtonItem *)rightBtnItem
{
    if (!_rightBtnItem) {
        _rightBtnItem = [[UIBarButtonItem alloc] initWithTitle:KNSLocal(@"Save") style:UIBarButtonItemStylePlain target:self action:@selector(processSaveItem)];
        [_rightBtnItem setTitleTextAttributes:@{NSFontAttributeName:PFR_FONT(16), NSForegroundColorAttributeName :TINT_GREEN_COLOR} forState:UIControlStateNormal];
    }
    return _rightBtnItem;
}

-(UITextField *)textField
{
    if (!_textField) {
        
        UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 15, CGRectGetWidth(self.view.bounds), 44)];
        backView.backgroundColor = WHITE_COLOR;
        [self.view addSubview:backView];
        
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0,  CGRectGetWidth(self.view.bounds) - 20, 44)];
        _textField.font = PFR_FONT(14);
        _textField.delegate = self;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.returnKeyType = UIReturnKeyDone;
        

        if (self.modifyMineInfoType == ModifyNickname) {
    
            _textField.text = [UserInfoManger shared].contact.nickName;
            _textField.placeholder = KNSLocal(@"ModifyPlaceholder_Content");
            [_textField inputTextMaxLength:KMaxNum];
            
        }else if (self.modifyMineInfoType == ModifyWXUsername){
            
            _textField.text = [UserInfoManger shared].contact.WXUserName;
            _textField.placeholder = KNSLocal(@"ModifyPlaceholder_nil");
            [_textField inputTextMaxLength:20];
        }

        [backView addSubview:_textField];
    }
    return _textField;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initWithUserInterface];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self showKeyBoard];
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self hiddenKeyBoard];
}

-(void)initWithUserInterface
{
    self.view.backgroundColor = [UIColor colorWithHex:0xefeff4 alpha:1.0];
    
    self.navigationItem.rightBarButtonItem = self.rightBtnItem;
    
    if (self.modifyMineInfoType == ModifyNickname) {
        self.title = KNSLocal(@"nameTitle");
    }else if (self.modifyMineInfoType == ModifyWXUsername){
        self.title = KNSLocal(@"WXName");
    }
    
    @weakify(self);
    [self.textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        @strongify(self);
        [self verifyTetxFieldContent];
    }];
    
}

#pragma mark - 验证输入内容
-(void)verifyTetxFieldContent
{
    BOOL enabled = NO;
    if (self.modifyMineInfoType == ModifyNickname) {
        
        if (self.textField.text.length >= KMinNum &&
            self.textField.text.length <= KMaxNum &&
            [Verificationlogic checkNickName:self.textField.text]){
            enabled = YES;
        }
    }else if (self.modifyMineInfoType == ModifyWXUsername){
        if (self.textField.text.length <= 20){
            enabled = YES;
        }
    }

    [self setSaveItemIsOk:enabled];
}

#pragma mark - 保存
-(void)processSaveItem
{
    [self setSaveItemIsOk:NO];
    [self hiddenKeyBoard];
    [self showLoadingHudWithHText:nil];
    @weakify(self);
    if (self.modifyMineInfoType == ModifyNickname) {
        [[UserInfoManger shared] saveUserNickName:self.textField.text complete:^(BOOL success,NSString * error) {
            @strongify(self);
            if (success) {
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [UITool showHudWithText:error withView:self.view];
            }
            [self setSaveItemIsOk:YES];
            [self hiddenLoadingHud];
        }];
    }else if (self.modifyMineInfoType == ModifyWXUsername){
        [[UserInfoManger shared] saveUserWXUserName:self.textField.text complete:^(BOOL success,NSString * error) {
            @strongify(self);
            if (success) {
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [UITool showHudWithText:error withView:self.view];
            }
            [self setSaveItemIsOk:YES];
            [self hiddenLoadingHud];
        }];
    }
}

#pragma mark - 输入框响应return
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self hiddenKeyBoard];
    return YES;
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hiddenKeyBoard];
}

-(void)returnToBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 收回键盘
-(void)hiddenKeyBoard
{
    [self.textField resignFirstResponder];
}
#pragma mark - 显示键盘
-(void)showKeyBoard
{
    [self.textField becomeFirstResponder];
}

-(void)setSaveItemIsOk:(BOOL)ok
{
    if (ok) {
        self.rightBtnItem.enabled = YES;
        [self.rightBtnItem setTitleTextAttributes:@{NSFontAttributeName:PFR_FONT(16), NSForegroundColorAttributeName :TINT_GREEN_COLOR} forState:UIControlStateNormal];
    }else{
        self.rightBtnItem.enabled = NO;
        [self.rightBtnItem setTitleTextAttributes:@{NSFontAttributeName:PFR_FONT(16), NSForegroundColorAttributeName :TINT_GREEN_TR_COLOR} forState:UIControlStateNormal];
    }
}

@end
